package org.example.lottery.common.pojo;


import lombok.AllArgsConstructor;
import lombok.Data;
import org.example.lottery.common.errorcode.ErrorCode;
import org.example.lottery.common.errorcode.GlobalErrorCodeConstants;
import org.springframework.util.Assert;

import java.io.Serializable;
import java.lang.reflect.Member;
@AllArgsConstructor
@Data
public class CommonResult <T>implements Serializable {
    private String message;
    private Integer code;
    private T data;

    public CommonResult(){

    }
    public CommonResult(Integer code,String msg){
        this.code=code;
        this.message= msg;
    }

    //定义结果
    public static <T>CommonResult<T> success(T data){
        CommonResult<T> commonResult=new CommonResult<T>();
        commonResult.message= GlobalErrorCodeConstants.SUCCESS.getMsg();
        commonResult.code=GlobalErrorCodeConstants.SUCCESS.getCode();
        commonResult.data=data;
        return commonResult;
    }

    public static <T> CommonResult<T> error(Integer code,String msg){
        Assert.isTrue(!GlobalErrorCodeConstants.SUCCESS.getCode().equals(code), "code 不是错误的异常");
        CommonResult<T> result=new CommonResult<>();
        result.code=code;
        result.message=msg;
        return result;
    }

    public static <T>CommonResult<T> error(ErrorCode errorCode){
        return error(errorCode.getCode(),errorCode.getMsg());
    }




}
